#include "stdafx.h"
#include <iostream>
using namespace std;

DWORD start,end;
const int TEST_SIZE = 20*1024*1024;    //the size of test file.
const int BUF_SIZE = 1024;

class Storage
{
public:
	Storage(char *c);
	int test_write();
	int test_read();
	char* test_file;
};

Storage::Storage(char *c)
{
	this->test_file=c;
}
int Storage::test_write()
{
	cout<<"Test size: "<<TEST_SIZE<<"kb"<<endl;
	try
	{
		FILE* fp = fopen(this->test_file,"wb");
		if (!fp)
		{
			cout<<"WRITE ERROR!"<<endl;
			return 1;
		}
		start = GetTickCount();
		for (int i=0;i < TEST_SIZE/BUF_SIZE;i++)
		{
			char tmp[BUF_SIZE];
			fwrite(tmp,BUF_SIZE,1,fp);
		}
		end = GetTickCount();
		fclose(fp);
	}
	catch(...)
	{
		cout<<"Unexpected error."<<endl;
		return 1;
	}
	cout<<"Write Speed:"<<TEST_SIZE/(end-start)/1.024<<"kb/s"<<endl;
	return 0;
}

int Storage::test_read()
{
	try
	{
		char buf[BUF_SIZE];
		FILE* fp = fopen(this->test_file,"rb");
		if (!fp)
		{
			cout<<"READ ERROR!"<<endl;
			return 1;
		}
		start = GetTickCount();
		int len = 0;
		do 
		{
			len = fread(buf,1,BUF_SIZE,fp);
		} while (len != 0);
		end = GetTickCount();
		fclose(fp);	
	}
	catch(...)
	{
		return 1;
	}
	cout<<"Read Speed:"<<TEST_SIZE/(end-start)/1.024<<"kb/s"<<endl;
	return 0;
}
